package com.exam.controller.exam;

import com.exam.common.Result;
import com.exam.common.SuperController;
import com.exam.dao.ExamUserDao;
import com.exam.pojo.param.ExamParam;
import com.exam.service.ExamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Description  (Exam)表控制层
 * @author ChengKe
 * @Date  2023-01-22
 */
@Api(tags = "Exam")
@RestController
@RequestMapping("/exam")
public class ExamController extends SuperController {

    @Resource
    private ExamService examService;

    @Autowired
    private ExamUserDao examUserDao;
    @Autowired
    StringRedisTemplate stringRedisTemplate;


    @ApiOperation("分页查询")
    @PostMapping("/{pageNum}/{pageSize}")
    public Result queryByPage(@RequestBody ExamParam exam,
                              @PathVariable("pageNum") Integer pageNum,
                              @PathVariable("pageSize") Integer pageSize) {
        return success(this.examService.queryByPage(exam, pageNum, pageSize));
    }

    @ApiOperation("根据考试类型查询数据")
    @GetMapping("/examType/{type}")
    @Cacheable(value = "examType",key="#examType")
    public Result queryByExamType(@PathVariable("type") String examType) {
        return success(this.examService.queryByExamType(examType));
    }


    @ApiOperation("通过主键查询单条数据")
    @GetMapping("/{id}")
    public Result queryById(@PathVariable("id") String id) {
        return success(this.examService.queryById(id));
    }


    @ApiOperation("新增考试")
    @PostMapping
    @CacheEvict(value = "examType",allEntries = true)
    public Result add(@RequestBody ExamParam exam) {
        return success(this.examService.insert(exam));
    }


    @ApiOperation("修改考试")
    @PutMapping
    public Result edit(@RequestBody ExamParam exam) {
        return success(this.examService.update(exam));
    }


    @ApiOperation("删除考试")
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") String id) {
        return this.examService.deleteById(id);
    }

}

